<html>
<head>
    <title>Pull Websocket/JSON Client Test</title>
    <script type="text/javascript">
      var socket;
      function createWebSocket() {
        host = "ws://" + window.location.host + "/" + window.location.pathname;

        if(window.MozWebSocket)
          window.WebSocket=window.MozWebSocket;
        if(!window.WebSocket) {
          window.document.getElementById("error").value = "Your browser don't support webSocket!";
          return false;
        }
        socket = new WebSocket(host);
        socket.onopen = function() { alert('socket opened'); }
        socket.onclose = function() { alert('socket closed'); }
        socket.onerror = function() { alert('An error occurs'); }
        socket.onmessage = onMessage;
      }

      function onMessage(msg){
        var response = JSON.parse(msg.data);
        if (response[0] == "onReception") alert(response[1]);
      }

      function sendMessage() {
        var data = ["onMethod", "websocket msg"];
        socket.send(JSON.stringify(data));
      }

      createWebSocket();
    </script>
</head>
<body>
  <input type="button" value="Send" onclick="sendMessage();" />
</body>
</html>
